class Solution:
    def backspaceCompare(self, S: str, T: str) -> bool:
        ns=1
        nt=1
        soffset=0
        toffset=0
        while ns<=len(S) or nt<=len(T):
            if ns<=len(S):
                if S[-ns]=="#":
                    soffset+=1
                    ns+=1
                    continue
                if S[-ns]!="#" and soffset!=0:
                    soffset-=1
                    ns+=1
                    continue
            if nt<=len(T):
                if T[-nt]=="#":
                    toffset+=1
                    nt+=1
                    continue
                if T[-nt]!="#" and toffset!=0:
                    toffset-=1
                    nt+=1
                    continue
            try:
                if S[-ns]!=T[-nt]:
                    return False
            except:
                return False
            nt+=1
            ns+=1
        if len(S)-ns!=len(T)-nt:
            return False
        return True

s=Solution()
print(s.backspaceCompare("bxj##tw",
"bxj###tw"))